/*
 * Copyright (C) 2010 Moduad Co., Ltd.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package cc.mango.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/** 
 * Utility class to obtain the values from configruation.
 *
 * @author Sehwan Noh (devnoh@gmail.com)
 */
public class Config /*extends Properties*/
{
	/**
	 * 
	 */
	private static final Log			log				= LogFactory
														.getLog(Config.class);
	private static final long		serialVersionUID	= 1L;
	private static Config			instance;
	private static String			configAbsolutePath;
	private static PropertyFileCache	propertyFileCache	= null;

	public synchronized static Config getInstance(String configAbsolutePath)
	{
		if (instance == null)
		{
			instance = new Config();
			Config.configAbsolutePath = configAbsolutePath;
		}
		if (propertyFileCache == null)
		{
			propertyFileCache = PropertyFileCache.getInstance();
		}
		return instance;
	}

	private Config()
	{
		/*try {
				super.load(Config.class.getResourceAsStream("/cc/mango/conf/config.properties"));
			} catch (IOException e) {
				e.printStackTrace();
			}*/
		init();
	}

	private void init()
	{

	}

	public String getString(String key)
	{
		return propertyFileCache.get(configAbsolutePath, key);
		//return instance.getProperty(key);
	}

	public String getString(String key, String def)
	{
		if (!propertyFileCache.containsKey(configAbsolutePath, key))
		{
			return def;
		}
		else
		{
			return propertyFileCache.get(configAbsolutePath, key);
		}
		//return  instance.getProperty(key,def);
	}

	public int getInt(String key)
	{
		//return Integer.parseInt(instance.getProperty(key));
		return Integer.parseInt(propertyFileCache
				.get(configAbsolutePath, key));
	}

	public int getInt(String key, int def)
	{
		//return Integer.parseInt(instance.getProperty(key,""+def));

		if (!propertyFileCache.containsKey(configAbsolutePath, key))
		{
			return def;
		}

		return Integer.parseInt(propertyFileCache
				.get(configAbsolutePath, key));

	}

	public long getLong(String key)
	{
		return Long.parseLong(propertyFileCache.get(configAbsolutePath, key));
		// return Long.parseLong(instance.getProperty(key));
	}

	public long getLong(String key, long def)
	{
		if (!propertyFileCache.containsKey(configAbsolutePath, key))
		{
			return def;
		}
		return Long.parseLong(propertyFileCache.get(configAbsolutePath, key));
		//  return Long.parseLong(instance.getProperty(key,""+def));
	}

	public float getFloat(String key)
	{
		return Float.parseFloat(propertyFileCache
				.get(configAbsolutePath, key));
		//return Float.parseFloat(instance.getProperty(key));
	}

	public float getFloat(String key, float def)
	{
		if (!propertyFileCache.containsKey(configAbsolutePath, key))
		{
			return def;
		}
		return Float.parseFloat(propertyFileCache
				.get(configAbsolutePath, key));
	}

	public double getDouble(String key)
	{
		//  return Double.parseDouble(instance.getProperty(key));
		return Double.parseDouble(propertyFileCache.get(configAbsolutePath,
				key));
	}

	public double getDouble(String key, double def)
	{
		if (!propertyFileCache.containsKey(configAbsolutePath, key))
		{
			return def;
		}
		return Double.parseDouble(propertyFileCache.get(configAbsolutePath,
				key));
	}

	public boolean getBoolean(String key)
	{
		// return Boolean.parseBoolean(instance.getProperty(key));
		return Boolean.parseBoolean(propertyFileCache.get(configAbsolutePath,
				key));
	}

	public boolean getBoolean(String key, boolean def)
	{
		if (!propertyFileCache.containsKey(configAbsolutePath, key))
		{
			return def;
		}
		return Boolean.parseBoolean(propertyFileCache.get(configAbsolutePath,
				key));
	}

}
